$KCODE = 'u'
require 'find'

# 通过读取java文件来产生对应的属性名称的声明.
# 
# @author: jun tsai
# @version $Revision: 9 $
# @since 0.0.1
# 
def firstCap str
  pro = ""
    pro << str[0,1].downcase
    pro << str[1,str.length]
    pro
end
def firstUp str
  pro = ""
    pro << str[0,1].upcase
    pro << str[1,str.length]
    pro
end

dir =  File.dirname(__FILE__)+'/../src/main/java/lichen/entities'
Find.find(dir)  do |file|
  next unless file =~/\.java$/
  
  i=0
  pros=[]

  last_l=0
  File.open(file).each{|line|
    i+=1
    if line =~ /private[\s]+([\w<>?]+)[\s]+([\w]+);/
      pros << $2
    elsif line =~/\}/
      last_l = i
    end
 }
 p pros
  next if pros.length == 0
  puts file
  override = true
  tmp_file_name=file+'.tmp'
  tmp_file=File.open(tmp_file_name,'w')

  j=0
  start_properties = false
  end_properties = false
  File.open(file).each{|line|
   j +=1
    if line =~ /\/\* bean properties begin \*\//
      start_properties=true
    elsif start_properties && line =~ /\/\* bean properties end \*\//
      end_properties = true 
      line="";
    end

    if(j == last_l)
      start_properties = false
      tmp_file<< "    /* bean properties begin */\n"
      pros.each{|str|
        tmp_str=firstUp(str)
        pro_name_var=""
        tmp_str.scan(/[A-Z][a-z0-9]*/){|s|
          pro_name_var+= s.upcase+"_"
        }
        pro_name_var=pro_name_var[0,(pro_name_var.length-1)]
        tmp_file<< "    public static final String "+pro_name_var+"_PRO_NAME=\"#{str}\";\n"
      }
       tmp_file<< "    /* bean properties end */\n"    
    end
    tmp_file << line unless start_properties && !end_properties
  }
    
   tmp_file.close
  
  File.delete(file) if override    
  File.rename(tmp_file_name,file) if override
  File.delete(tmp_file_name) if File.exist?(tmp_file_name)
end